﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace UnitDesktop
{
    public partial class Form1 : Office2007Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ServiceReference.ServiceSoapClient obj = new ServiceReference.ServiceSoapClient();

        private int rowUnit,rowRate;
        private string[] UnitId;
        
        private string choosingItem1="", choosingItem2="";
        private List<string> ListUnit = new List<string>();
        private List<string> ListChildUnitName = new List<string>();
        private List<string> ListChildUnit = new List<string>();
        private List<string> ListRateName = new List<string>();
        private List<string> ListRate = new List<string>();
        private List<Unit> UnitTypeName = new List<Unit>();
        private List<Result> RateResult = new List<Result>();
        private void Form1_Load(object sender, EventArgs e)
        {

            try
            {
                ListUnit = obj.getListUnit();

                UnitId = new string[obj.getCount()];

                rowRate = 0;
                rowUnit = 0;                        //So dong trong bang quan he ten tab, noi dung

                int count = obj.getCount();
                for (int i = 0; i < ListUnit.Count; i++)
                {
                    //  MessageBox.Show(obj.test(i));

                    ListBox ListBox1 = new ListBox();

                    ListBox1.Location = new System.Drawing.Point(6, 19);
                    ListBox1.Name = "listBox1";
                    // ListBox1a.View = View.List;
                    ListBox1.Size = new System.Drawing.Size(223, 160);
                    ListBox1.TabIndex = 0;


                    ListBox ListBox2 = new ListBox();
                    //listView2.Items.Add(item);
                    ListBox2.Location = new System.Drawing.Point(6, 19);
                    ListBox2.Name = "listBox2";
                    ListBox2.Size = new System.Drawing.Size(223, 160);
                    ListBox2.TabIndex = 1;

                    GroupBox group1a = new GroupBox();
                    group1a.Controls.Add(ListBox1);
                    group1a.Name = "Input";
                    group1a.Text = "Input";
                    group1a.Size = new System.Drawing.Size(235, 193);
                    group1a.Location = new System.Drawing.Point(6, 18);

                    GroupBox group1b = new GroupBox();
                    group1b.Controls.Add(ListBox2);
                    group1b.Name = "Output";
                    group1b.Text = "Output";
                    group1b.Size = new System.Drawing.Size(235, 193);
                    group1b.Location = new System.Drawing.Point(300, 18);

                    TabPage tab1 = new TabPage();
                    tab1.Controls.Add(group1a);
                    tab1.Controls.Add(group1b);
                    tab1.Name = "tab" + obj.getUnitId(i).ToString();
                    tab1.Text = ListUnit[i];
                    tab1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));

                    tabControl.Controls.Add(tab1);


                    UnitId[i] = obj.getUnitId(i);

                    ListChildUnit.Clear();
                    ListChildUnitName.Clear();

                    ListChildUnit = obj.getListChildUnit(i);
                    ListChildUnitName = obj.getListChildUnitName(i);
                    for (int j = 0; j < ListChildUnit.Count; j++)
                    {
                        Unit unit = new Unit();
                        unit.ID = ListChildUnit[j].ToString();
                        unit.Name = ListChildUnitName[j].ToString();
                        UnitTypeName.Add(unit);

                        rowUnit++;

                        ListViewItem item = new ListViewItem();
                        item.Name = ListChildUnit[j];
                        item.Text = ListChildUnit[j];
                        ListBox1.Items.Add(item.Text);
                        ListBox2.Items.Add(item.Text);

                        ListRateName.Clear();
                        ListRate.Clear();

                        ListRateName = obj.getListRateName(i, j);
                        ListRate = obj.getListRate(i, j);

                        for (int k = 0; k < ListRate.Count; k++)
                        {
                            Result res = new Result();
                            res.UnitName = ListUnit[i];
                            res.Name1 = ListChildUnitName[j];
                            res.Name2 = ListRateName[k];
                            res.Rate = ListRate[k];
                            RateResult.Add(res);
                            rowRate++;
                        }


                    }

                    ListBox1.SelectedIndexChanged += new System.EventHandler(ListBox1_SelectedIndexChanged);
                    ListBox2.SelectedIndexChanged += new System.EventHandler(ListBox2_SelectedIndexChanged);

                }
            }
            catch
            {
                MessageBox.Show("Chua ket noi duoc server!");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox ListBox1 = sender as ListBox;
            ListViewItem item = ListBox1.Tag as ListViewItem;
            try
            {
                choosingItem1 = ListBox1.SelectedItem.ToString();
            }
            catch { }
            string temp1 = "", temp2 = "";
            float result;
            if (choosingItem1 != "" && choosingItem2 != "")
            {
                if (textBox1.Text == "") textBox1.Text = "1";
                for (int i = 0; i < rowUnit; i++)
                {
                    if (UnitTypeName[i].ID == choosingItem1)
                    {
                        temp1 = UnitTypeName[i].Name;
                    }
                    if (UnitTypeName[i].ID == choosingItem2)
                    {
                        temp2 = UnitTypeName[i].Name;
                    }
                }
            }
            if (temp1 != "" && temp2 != "")
            {
                for (int i = 0; i < rowRate; i++)
                {
                    if (temp1 == RateResult[i].Name1 && temp2 == RateResult[i].Name2)
                    {
                        try
                        {
                            result = float.Parse(textBox1.Text) * float.Parse(RateResult[i].Rate);
                            textBox2.Text = result.ToString();
                            label1.Text = choosingItem1;
                            label2.Text = choosingItem2;
                        }
                        catch
                        {
                            MessageBox.Show("Moi nhap vao gia tri!");
                        }
                    }

                }
                temp1 = ""; temp2 = "";
            }
        }

        private void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox ListBox2 = sender as ListBox;
            ListViewItem item = ListBox2.Tag as ListViewItem;
            try
            {
                choosingItem2 = ListBox2.SelectedItem.ToString();
            }
            catch { }
            string temp1 = "", temp2 = "";
            float result;
            if (choosingItem1 != "" && choosingItem2 != "")
            {
                if (textBox1.Text == "") textBox1.Text = "1";
                for (int i = 0; i < rowUnit; i++)
                {
                    if (UnitTypeName[i].ID == choosingItem1)
                    {
                        temp1 = UnitTypeName[i].Name;
                    }
                    if (UnitTypeName[i].ID == choosingItem2)
                    {
                        temp2 = UnitTypeName[i].Name;
                    }
                }
            }
            if (temp1 != "" && temp2 != "")
            {
                for (int i = 0; i < rowRate; i++)
                {
                    if (temp1 == RateResult[i].Name1 && temp2 == RateResult[i].Name2)
                    {
                        try
                        {
                            result = float.Parse(textBox1.Text) * float.Parse(RateResult[i].Rate);
                            textBox2.Text = result.ToString();
                            label1.Text = choosingItem1;
                            label2.Text = choosingItem2;
                        }
                        catch
                        {
                            MessageBox.Show("Moi nhap vao gia tri!");
                        }
                    }

                }
                temp1 = ""; temp2 = "";
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string temp1 = "", temp2 = "";
            float result;
            if (choosingItem1 != "" && choosingItem2 != "")
            {
                for (int i = 0; i < rowUnit; i++)
                {
                    if (UnitTypeName[i].ID == choosingItem1)
                    {
                        temp1 = UnitTypeName[i].Name;
                    }
                    if (UnitTypeName[i].ID == choosingItem2)
                    {
                        temp2 = UnitTypeName[i].Name;
                    }
                }
            }
            if (temp1 != "" && temp2 != "")
            {
                for (int i = 0; i < rowRate; i++)
                {
                    if (temp1 == RateResult[i].Name1 && temp2 == RateResult[i].Name2)
                    {
                        try
                        {
                            result = float.Parse(textBox1.Text) * float.Parse(RateResult[i].Rate);
                            textBox2.Text = result.ToString();
                            label1.Text = choosingItem1;
                            label2.Text = choosingItem2;
                        }
                        catch
                        {
                            //MessageBox.Show("Moi nhap vao gia tri!");
                        }
                    }

                }
                temp1 = ""; temp2 = "";
            }
        }
    }
}